package co.edu.senaclem.novetrapp;


import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.VideoView;

public class ActivityVideo extends Activity implements OnCompletionListener{

	public VideoView videoViewVideo;
	public Uri uri;
	public ToggleButton toggleButtonPlayPausa;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		
		Bundle recibido = getIntent().getExtras();
        //luego obtenemos el extra que queremos, recuerde que el extra se llamaba 'nombre'
        String opcion=recibido.getString("opcion");
		
		videoViewVideo = (VideoView) findViewById(R.id.videoViewVideo); 
		toggleButtonPlayPausa= (ToggleButton) findViewById(R.id.toggleButtonPlayPausa);
		findViewById(R.id.toggleButtonPlayPausa).setOnClickListener(eventoToggleButtonPlayPausa);
        
		gestionarVideo(opcion);
	}
	
	public void gestionarVideo(String opcion)
	{			
		try
        {
			if(opcion.equals("Traslado Centro"))
				uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.trasladocentro); //Gets video path inside our project
			else if(opcion.equals("Traslado Programa"))
				uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.trasladoprograma); //Gets video path inside our project
			else if(opcion.equals("Traslado Jornada"))
				uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.trasladojornada); //Gets video path inside our project
			else if(opcion.equals("Retiro Voluntario"))
				uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.retiro); //Gets video path inside our project
			else
				uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.aplazamiento); //Gets video path inside our project
						
			
            videoViewVideo.setVideoURI(uri);  //Associates path with videoView
        	//Sets a preview snapshot of the video
            videoViewVideo.setOnCompletionListener(this);											  //Sets a completion listener to stop
            if(!videoViewVideo.isPlaying())
            	videoViewVideo.start();
            else
            	videoViewVideo.pause();
        }
        catch(Exception e)
        {
        	Toast.makeText(getApplicationContext(), "Revise sus codecs de Video", Toast.LENGTH_LONG).show();
        }  
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		videoViewVideo.stopPlayback();
	}
	
	View.OnClickListener eventoToggleButtonPlayPausa = new View.OnClickListener() {
        public void onClick(View v) {
        	 if(toggleButtonPlayPausa.isChecked())
        		 videoViewVideo.pause();
        	 else
        		 videoViewVideo.start();
        		 
        }
    };


}
